/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package edu.mobbuzz.page.menu;

import com.sun.lwuit.Command;
import com.sun.lwuit.Component;
import com.sun.lwuit.Container;
import com.sun.lwuit.Display;
import com.sun.lwuit.Font;
import com.sun.lwuit.Form;
import com.sun.lwuit.Label;
import com.sun.lwuit.List;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.layouts.BorderLayout;
import com.sun.lwuit.layouts.BoxLayout;
import com.sun.lwuit.list.DefaultListModel;
import com.sun.lwuit.list.ListCellRenderer;
import com.sun.lwuit.plaf.Border;
import edu.mobbuzz.page.menu.rsschannel.RSSChannelMenu;
import edu.mobbuzz.renderer.MenuRenderer;
import edu.mobbuzz.util.string.MenuString;
import java.util.Vector;

/**
 *
 * @author dailicious
 */
public class MainMenu extends BaseMenu {
    private Form menuForm;
    private Container menuPanel;
    private Label menuTitle;
    private DefaultListModel menuListModel;
    private Vector menuArray;
    private List menuList;

    public MainMenu(){
        menuForm = new Form();
        menuPanel = new Container();
        menuTitle = new Label(getMenuTitle());   
        menuListModel = new DefaultListModel(displayMenuList());
        
    }

    public void showPage() {
        menuTitle.setAlignment(Component.CENTER);
        menuTitle.getStyle().setFont(Font.createSystemFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD, Font.SIZE_SMALL));
        menuTitle.getStyle().setBgColor(0x739ABD);
        
        menuPanel.setLayout(new BoxLayout((BoxLayout.Y_AXIS)));
        menuPanel.addComponent(menuTitle);
        menuPanel.addComponent(createList(menuListModel, List.VERTICAL, new MenuRenderer()));

        menuForm.addCommand(getExitCmd());
        menuForm.addCommand(getOnlineCmd());
        menuForm.addCommand(new Command("Open") {
            public void actionPerformed(ActionEvent evt) {
               
            }
        });

        menuForm.setLayout(new BorderLayout());
        menuForm.addComponent(BorderLayout.CENTER, menuPanel);
        menuForm.setScrollable(false);
        menuForm.setCommandListener(getActionlistener());
        menuForm.show();
    }

    public void gameActionPerformed(int keyCode){
        int game = Display.getInstance().getGameAction(keyCode);

        if(game == Display.GAME_FIRE){
            String _menu = (String) menuList.getSelectedItem();
            if(_menu.equals(MenuString.INBOX)){

            }else if(_menu.equals(MenuString.OUTBOX)){

            }else if(_menu.equals(MenuString.RSSCHANNEL)){
                openRSSChannel();
            }else if(_menu.equals(MenuString.CATEGORY)){

            }else if(_menu.equals(MenuString.CONTACT)){

            }

        }else if(game == Display.GAME_DOWN){
            if(menuList.getSelectedIndex()<(menuList.size()-1)){
               menuList.setSelectedIndex(menuList.getSelectedIndex()+1);
            }else{
                menuList.setSelectedIndex(0);
            }

        }else if(game == Display.GAME_UP){
             if(menuList.getSelectedIndex()!=0){
                 menuList.setSelectedIndex(menuList.getSelectedIndex()-1);
             }else{
                 menuList.setSelectedIndex(menuList.size()-1);
             }

        }
    }

    public void openRSSChannel() {
        RSSChannelMenu rssChannelPage = new RSSChannelMenu();
        rssChannelPage.setExitCmd(getExitCmd());
        rssChannelPage.setActionlistener(getActionlistener());
        rssChannelPage.showPage();
	}

	public void openInbox() {

	}

	public void openMenu() {

	}

	public void openCategory() {

	}

	public void openContact() {

	}
    
    private List createList(DefaultListModel menuListModel, int orientation, ListCellRenderer renderer) {
        menuList = new List(menuListModel){
            public void keyPressed(int keyCode){
               gameActionPerformed(keyCode);
            }
        };
        menuList.getStyle().setBgTransparency(0);
        menuList.getStyle().setBorder(Border.getEmpty());
        menuList.setListCellRenderer(renderer);
        menuList.setOrientation(orientation);

        return menuList;
    }
    
    public Vector displayMenuList() {
        menuArray = new Vector();
        menuArray.addElement(MenuString.INBOX);
        menuArray.addElement(MenuString.OUTBOX);
        menuArray.addElement(MenuString.RSSCHANNEL);
        menuArray.addElement(MenuString.CATEGORY);
        menuArray.addElement(MenuString.CONTACT);


        return menuArray;
    }
    
    public String getMenuTitle() {
        return MenuString.MENU;
    }

    
}
